package com.hmkj.web.controller.my;

import com.hmkj.common.baseMapper.GenericPo;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.page.BasePageParams;
import com.hmkj.common.page.Pages;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.mapper.operate.QuestionsMapper;
import com.hmkj.core.model.operate.QuestionsModel;
import com.hmkj.core.model.user.UserModel;
import com.hmkj.core.po.operate.Questions;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.operate.QuestionsService;
import com.hmkj.web.controller.GenericController;
import com.xiaoleilu.hutool.util.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Api(description = "在线问答相关")
@Slf4j
@RestController
@RequestMapping(GenericController.MEMBER_URL + "/questions")
public class MyQuestionsController extends GenericController<Integer, Questions> {

    @Resource
    private QuestionsService questionsService;

    @ApiOperation(value = "在线提问列表", notes = "在线提问列表")
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ResultEntity<Pages<QuestionsModel>> list(@ModelAttribute BasePageParams basePageParams) {
        User user = getLoginUser();
        Map<String, Object> param = new HashMap<>();
        param.put("userId", user.getId());
        param.put("basePageParams", basePageParams);
        Pages<QuestionsModel> pages = questionsService.selectByPage(basePageParams,
                q -> ((QuestionsMapper) q).findCountQuestionsForList(param),
                q -> ((QuestionsMapper) q).findListForQuestionsList(param));
        //list排序
        pages.setRows(pages.getRows().stream().sorted(Comparator.comparing(QuestionsModel::getCreateTime)).collect(Collectors.toList()));
        return ok(pages);
    }

    @ApiOperation(value = "在线提问", notes = "在线提问")
    @ResponseBody
    @RequestMapping(value = "putQuestion", method = RequestMethod.POST)
    public ResultEntity putQuestion(@ApiParam("提问内容") @RequestParam String question) {
        User user = getLoginUser();
        if(!StringUtils.isNotBlank(question)){
            throw new BussinessException("提问内容不能为空");
        }
        Questions q = new Questions();
        q.setQuestion(question);
        q.setAgentId(user.getAgentId());
        q.setUserId(user.getId());
        q.setStatus(Questions.STATUS.T1.code);
        q.setDelFlag(Questions.DELFLAG.NORMAL.code);
        q.setReplyCost(0);
        q.setCreateTime(new Date());
        questionsService.insert(q);
        Questions questions= new Questions();
        questions.setQuestion(question);
        questions.setUserId(user.getId());
        List<Questions> list = questionsService.selectListByQuestions(questions);
        Questions qu = new Questions();
        if(CollectionUtil.isNotEmpty(list)){
            qu = list.get(0);
        }else{
            throw new BussinessException("查询问题不存在");
        }
        return ok(qu.getId());
    }


    @ApiOperation(value = "实时获取问题列表", notes = "实时获取问题列表")
    @ResponseBody
    @RequestMapping(value = "getQuestions", method = RequestMethod.POST)
    public ResultEntity getQuestions(@ApiParam("问题id") @RequestParam String[] ids) {
        User user = getLoginUser();
        List<Questions> questions = new ArrayList<>();
        if(ids ==null||ids.length == 0){
            return ok(questions);
        }
        questions  =  questionsService.selectListByIds(ids,user.getId());
        return ok(questions);
    }

}
